﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyOA.Model;
using MyOA.BLL;

namespace MyOA.UI.PersonManage
{
    public partial class CreateOrUpdateDepartInfo : System.Web.UI.Page
    {
        DepartInfoManager departInfoManager = new DepartInfoManager();
        BranchInfoManager branchInfoManager = new BranchInfoManager();
        string message = string.Empty;      //提示信息
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //未登录先登录
                if (Session["loginId"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                }
                //已登录则加载
                else
                {
                    LoadDepartInfo();
                    string departId = Request.QueryString["DepartId"];
                    if (departId != null)
                    {
                        DisplayDepartInfo(departId);
                    }
                }
            }
        }
        //绑定部门信息
        public void LoadDepartInfo()
        {
            IList<BranchInfo> branchInfoList = branchInfoManager.GetAllBranchInfo();
            foreach (BranchInfo branchInfo in branchInfoList)
            {
                ListItem li = new ListItem();
                li.Value = branchInfo.BranchId.ToString();
                li.Text = branchInfo.BranchName;
                ddlBranchInfo.Items.Add(li);

            }
        }
        //修改信息时显示信息
        public void DisplayDepartInfo(string departId)
        {
            int Id = Convert.ToInt32(departId);
            DepartInfo departInfo = departInfoManager.GetDepartInfoBuDepartInfoByDepartId(Id);
            this.ddlBranchInfo.SelectedValue = departInfo.Branch.BranchId.ToString();
            this.txtDepartEmail.Text = departInfo.DepartEmail;
            this.txtDepartPhone.Text = departInfo.DepartPhone.ToString();
            this.txtDepartName.Text = departInfo.DepartName;
        }
        //保存按钮
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DepartInfo departInfo = new DepartInfo();
            departInfo.DepartEmail = this.txtDepartEmail.Text;
            departInfo.DepartName = this.txtDepartName.Text;
            departInfo.DepartPhone = this.txtDepartPhone.Text;
            int branchId = Convert.ToInt32(this.ddlBranchInfo.SelectedValue);
            departInfo.Branch = branchInfoManager.GetBranchInfoByBranchId(branchId);
            //根据Request.QueryString["DepartId"]是否为空判断执行修改或删除
            if (Request.QueryString["DepartId"] != null)
            {
                int departId = Convert.ToInt32(Request.QueryString["DepartId"]);
                departInfo.DepartId = departId;

                int result = departInfoManager.UpdateDepartInfoByDepartId(departInfo);
                if (result > 0)
                {
                    message = "修改成功";
                }
                else
                {
                    message = "修改失败";
                }
            }
            else
            {

                //判断该机构中是否存在当前要添加的部门
                if (departInfoManager.getDepartInfoByBranchIdAndDepartName(branchId, departInfo.DepartName).DepartName == "")
                {
                    int result = departInfoManager.AddDepartInfo(departInfo);
                    if (result > 0)
                    {
                        message = "添加成功";
                    }
                    else
                    {
                        message = "添加失败";
                    }
                }
                else
                {
                    message = "对不起，该部门已存在！";
                }
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()');</script>", message);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);

        }
        //返回按钮
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("DepartManage.aspx");
        }
        //全部重写
        protected void Button1_Click(object sender, EventArgs e)
        {
            //this.txtDepartEmail.Text = " ";
            //this.txtDepartPhone.Text =" ";
            //this.txtDepartName.Text =" ";
        }
    }
}
